package map.coreui.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.baidu.mapapi.search.route.WalkingRouteLine
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup
import kotlin.math.min

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/24 16:01
 */
class MultiRouteView : BaseViewGroup {

    private val route1: MultiRouteItemView by lazy { findViewById(R.id.multi_route_1) }
    private val route2: MultiRouteItemView by lazy { findViewById(R.id.multi_route_2) }
    private val route3: MultiRouteItemView by lazy { findViewById(R.id.multi_route_3) }
    private val navi: Button by lazy { findViewById(R.id.multi_route_start_navi) }
    private val routeList: List<MultiRouteItemView> by lazy {
        ArrayList<MultiRouteItemView>().apply {
            this.add(route1)
            this.add(route2)
            this.add(route3)
        }
    }

    private var routeListData: List<WalkingRouteLine>? = null

    private var mListener: RouteSelectListener? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.multi_route_view
    }

    override fun onBindListener() {
        for (i in routeList.indices) {
            routeList[i].setOnClickListener {
                routeListData?.get(i)?.let { line ->
                    mListener?.onSelectRoute(line)
                }
            }
        }
        navi.setOnClickListener {
            mListener?.onNavi()
        }
    }

    override fun onBindValue() {}
    override fun onFindView(rootView: View) {}


    fun bindData(list: List<WalkingRouteLine>) {
        routeListData = list
        //先清除之前数据
        for (view in routeList) {
            view.visibility = View.GONE
        }
        //绑定数据
        for (i in 0 until min(routeList.size, list.size)) {
            routeList[i].apply {
                this.visibility = View.VISIBLE
                val line = list[i]
                this.bindData(line)
            }
        }
    }

    fun bindListener(listener: RouteSelectListener) {
        mListener = listener
    }


    interface RouteSelectListener {
        fun onSelectRoute(route: WalkingRouteLine)
        fun onNavi()
    }
}